//
//  HelperViewController.swift
//  JKTools
//
//  Created by 姜奎 on 2022/11/10.
//

import Cocoa
import Down
import WebKit

class HelperViewController: NSViewController {

    @IBOutlet weak var webView: WKWebView!
    lazy var indicator: NSProgressIndicator = {
        var indicator = NSProgressIndicator()
        indicator.translatesAutoresizingMaskIntoConstraints = false
        indicator.style = .bar
        indicator.isDisplayedWhenStopped = true
        indicator.usesThreadedAnimation = true
        indicator.isIndeterminate = true
        self.view.addSubview(indicator)
        indicator.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
        indicator.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
        indicator.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        indicator.heightAnchor.constraint(equalToConstant: 1).isActive = true
        return indicator
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
//        self.updateHelpMD()
//        self.download()

        guard let path = Bundle.main.path(forResource: "README", ofType: "md") else {
            return
        }

        guard let md = try? String(contentsOf: URL(fileURLWithPath: path)) else {
            return
        }

        // 创建 Down 实例
        let down = Down(markdownString: md)

        // 将 Markdown 转换为 HTML
        guard let html = try? down.toHTML() else {
            return
        }

        self.webView.loadHTMLString(html, baseURL: nil)
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
    }
    
    func startAnimation(){
        DispatchQueue.main.async {
            self.indicator.startAnimation(nil)
        }
    }
    
    func stopAnimation(){
        DispatchQueue.main.async {
            self.indicator.stopAnimation(nil)
            self.indicator.isHidden = true
        }//
    }
}
